// shop page
// ============

define(['zepto', 'sui', 'utils', 'wxshare'], 
function($, sui, utils, wxshare) {
  return {
    init: function(data) {
      wxshare.initShare(data.jsConfig, false)
      this.roundId = data.roundId;
      this.fuxing = data.fuxing;
      
      this.initSelectTime()
      this.initSubmit();
      this.initSelectArea();
    },
    
    initSelectTime: function() {
        var that = this;
        $(document).on("click", ".item.available a", function(e){
            $(".item.available").removeClass("active").find(".tips").html("可预约");
            $(this).parent().addClass("active").find(".tips").html("已选择");
            $(".submit-fixed").removeClass("hide");
            $(".submit-fixed").find(".date").html($(this).data("date"))
            $(".submit-fixed").find(".hour").html($(this).data("time"))
            that.date = $(this).data("datevalue");
            that.hour = $(this).data("time");
            $("input[name='hour']").val(that.hour);
            
            var common = $(this).data("common");
            var room = $(this).data("room");
            
            var commonName = that.fuxing == 'true' ? "米域" : "公共区域";
            $(".area-type-select").find(".common").append(
              commonName + "(" + (common == -1 ? '' : common == 0 ? '已满' : common) + ")"
            )
            $(".area-type-select").find(".room").append("私人区域(" + (room == -1 ? '' : room == 0 ? '已满' : room) + ")")
            if(common != 0 && common == 0) {
            	$(".area-type-select").find(".common").addClass("full disabled");
            	$(".area-type-select").find(".common").find('input[name="common"]').prop("disabled", true);
            }
            if(room != 0 && room == 0) {
            	$(".area-type-select").find(".room").addClass("full disabled");
            	$(".area-type-select").find(".room").find('input[name="room"]').prop("disabled", true);
            }
            
        })
    },
    
    initSelectArea: function() {
        var areaType = $("input[name='areaType']").val().trim();
        if(!areaType) {
            $(".area-type-select").show();
            $("#submit").hide();
            
        } else {
            $(".area-type-select").hide();
            $("#submit").show();
        }
        
        $(document).on("change", 'input[name="areaSelectType"]', function() {
            $("input[name='areaSelectType']:checked").parent().addClass('active').siblings().removeClass('active') 
            
            var areaVal = $("input[name='areaSelectType']:checked").val()
            $("input[name='areaType']").val(areaVal) 
            $("#submit").show();
        })
        
    },
    
    initSubmit: function() {
        var that = this;
        $("#submit").on("click", function(e){
          
            e.preventDefault();
            
            var hour = $("input[name='hour']").val();
            if(!hour) {
                return  $.toast('请选择时间');
            }
            
            $.showPreloader('正在提交预约')
            var $form = $("form[class='coach-form']");
            console.log($form.serializeArray())
            $.post($($form).attr("action"), $form.serializeArray()).done(
              function(response) {
                $.hidePreloader()
                utils.processResult(response, function(result) {
                  $.toast(result.message || "预约成功")
                  setTimeout(function() {
                    window.location.href = __CTX__.root + "/profile/courses/" + that.roundId;
                  }, 1000)
                }, function(result) {
                  $.toast(result.message || '预约失败')
               })
            })
          
        });
    }
    
  }
})